/******************************************************************************************

 Made by Flashphoner. All rights reserved.
 These sources can only be used for working with Flashphoner server.
 If you want use this to create your own application (separate from Flashphoner server)
 please contact support@flashphoner.com

 ******************************************************************************************/

package com.flashphoner.app;

import com.flashphoner.sdk.rtmp.AbstractRtmpClient;
import com.flashphoner.sdk.sip.SipHeader;
import com.flashphoner.sdk.softphone.ISoftphoneCall;
import com.flashphoner.sdk.rtmp.IConfig;
import com.flashphoner.sdk.rtmp.Config;

import com.flashphoner.sdk.softphone.exception.CrossCallException;
import com.flashphoner.sdk.softphone.exception.PortsBusyException;
import com.flashphoner.sdk.softphone.exception.SoftphoneException;
import com.wowza.wms.client.IClient;
import com.wowza.wms.logging.WMSLogger;
import com.wowza.wms.logging.WMSLoggerFactory;

import java.util.Timer;

public class RtmpClient extends AbstractRtmpClient {

    private Timer hangupTimer = null;

    private static WMSLogger log = WMSLoggerFactory
            .getLogger(RtmpClient.class);

    public RtmpClient(String login, String pwd, IClient client, String sipProviderAddress, int sipProviderPort, String visibleName, boolean regRequired, String appName, String mode) {
        super(login, pwd, client, sipProviderAddress, sipProviderPort, visibleName, regRequired, appName, mode);
    }

    public RtmpClient(String login, String pwd, IClient client, boolean regRequired, String appName, String mode) {
        super(login, pwd, client, regRequired, appName, mode);
    }

    public RtmpClient(final String login, final String pwd,
                      final IClient client, final String appName, String mode) {
        super(login, pwd, client, appName, mode);
    }

    public RtmpClient(final String login, final String pwd,
                      final boolean regRequired, final String appName, String mode) {
        super(login, pwd, regRequired, appName, mode);
    }

    public RtmpClient(final String login, final String pwd, final String appName, String mode) {
        super(login, pwd, appName, mode);
    }

    public void registered(SipHeader sipHeader) {
        log.info("RtmpClient.registered()");
        getClient().call("registered", null, sipHeader.toAMFObj());
    }

    public void notifyBalance(String balance, SipHeader sipHeader) {
        log.info("RtmpClient.notifyBalance()");
        getClient().call("notifyBalance", null, balance, sipHeader.toAMFObj());
    }

    public void ring(ISoftphoneCall call, SipHeader sipHeader) {
        log.info("RtmpClient.ring() " + call.getId());
        getClient().call("ring", null, call.toAMFDataObj(), sipHeader.toAMFObj());
    }

    public void ringing(ISoftphoneCall call, SipHeader sipHeader) {
        log.info("RtmpClient.ringing() " + call.getId());
    }

    public void sessionProgress(ISoftphoneCall call, SipHeader sipHeader) {
        log.info("RtmpClient.sessionProgress()");
        streamStart("INCOMING_" + login + "_" + call.getId());
        getClient().call("sessionProgress", null, call.toAMFDataObj(), sipHeader.toAMFObj());
    }

    public void talk(ISoftphoneCall call, SipHeader sipHeader) {
        log.info("RtmpClient.talk() " + call.getId());
        streamStart("INCOMING_" + login + "_" + call.getId());
        getClient().call("talk", null, call.toAMFDataObj(), sipHeader.toAMFObj());
        if (Config.DEMO) {
            if (hangupTimer == null) {
                hangupTimer = new Timer();
                hangupTimer.schedule(new HangupTimerTask(this), 30000);
            }
        }
    }

    public void finish(ISoftphoneCall call, SipHeader sipHeader) {
        log.info("RtmpClient.finish() " + call.getId());
        streamStop();
        getClient().call("finish", null, call.toAMFDataObj(), sipHeader.toAMFObj());
    }

    public void busy(ISoftphoneCall call, SipHeader sipHeader) {
        log.info("RtmpClient.busy() " + call.getId());
        getClient().call("busy", null, call.toAMFDataObj(), sipHeader.toAMFObj());
    }


    public ISoftphoneCall call(final String caller, final String callee, final String visibleName, final Boolean isVideoCall) throws SoftphoneException {
        log.info("RtmpClient.call() " + callee);
        return getSoftphone().call(caller, callee, visibleName, isVideoCall);
    }

    public void answer(final String callId, final Boolean isVideoCall) throws SoftphoneException {
        log.info("RtmpClient.answer() " + callId);
        getSoftphone().answer(callId, isVideoCall);
    }

    public void updateCallToVideo(final String callId) throws SoftphoneException {
        log.info("RtmpClient.updateCallToVideo() " + callId);
        getSoftphone().updateCallToVideo(callId);
    }


    public void hangup(final String callId) throws SoftphoneException {
        log.info("RtmpClient.hangup() " + callId);
        getSoftphone().hangup(callId);
        streamStop();
    }

    public void sendDtmf(final String callId, final String dtmf) throws SoftphoneException {
        log.info("RtmpClient.sendDtmf() " + dtmf);
        getSoftphone().sendDtmf(callId, dtmf);
    }

    public void fail(String errorCode, SipHeader sipHeader) {
        log.info("RtmpClient.fail() " + errorCode);
        if (sipHeader == null) {
            sipHeader = new SipHeader();
        }
        if (getClient() != null) {
            getClient().call("fail", null, errorCode, sipHeader.toAMFObj());
        }
    }

    public IConfig getConfig() {
        return Config.getInstance();
    }


}
